package cn.cxion.chat.app.view.popupview;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.lxj.xpopup.core.CenterPopupView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.AppointmentInfo;
import cn.cxion.chat.app.bean.AppointmentTimes;
import cn.cxion.chat.app.util.StringUtil;

public class TimePickPopupView extends CenterPopupView {


    @Bind(R.id.time)
    TimePicker time;
    @Bind(R.id.cancle)
    TextView cancle;
    @Bind(R.id.ok)
    TextView ok;
    int mHour;
    int mMinute;
    OnTimeSelectListener listener;
    AppointmentTimes appointmentTimes;

    public TimePickPopupView(@NonNull Context context) {
        super(context);

    }

    public TimePickPopupView(@NonNull Context context, AppointmentTimes appointmentTimes) {
        super(context);
        if (appointmentTimes != null)
            this.appointmentTimes = appointmentTimes;
        else
            this.appointmentTimes = new AppointmentTimes();

    }

    public void setOnTimeSelectListener(OnTimeSelectListener listener) {
        this.listener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_timepicker;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ButterKnife.bind(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onShow() {
        super.onShow();
        if(!StringUtil.isEmpty(appointmentTimes.getHourText())) {
            mHour = appointmentTimes.getHour();
            mMinute = appointmentTimes.getMinute();
            time.setHour(mHour);
            time.setMinute(mMinute);
        }

        time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
            }
        });
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @OnClick({R.id.cancle, R.id.ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle:
                dismiss();
                break;
            case R.id.ok:
                appointmentTimes.setHour(mHour);
                appointmentTimes.setMinute(mMinute);
                appointmentTimes.setHourText(mHour+":"+(mMinute<10?("0"+mMinute):mMinute));
                listener.select(appointmentTimes);
                dismiss();
                break;
        }
    }

    public interface OnTimeSelectListener {
        void select(AppointmentTimes appointmentTimes);
    }

}
